<?php

namespace App\Models;

use App\Models\User;
use App\Models\UserLabel;
use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\Filters;

class Shop extends Model
{
    use Filters; 

    protected $table = 'shops';
    /**
     * 不可以批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [];
    // const per_page=2;//每页条数

    public function transform_crowdFunding_list()
    {
        $img_arr = [];
        if (is_array($this->shop_info)) {
            foreach ($this->shop_info as $value) {
                $img_arr[] = config('app.img_url') . $value;
            }
        }
        if(count($this->shopbrowse)>4){
            $crowds = $this->shopbrowse->random(4)->pluck('user_id');
        }else{
            $crowds = $this->shopbrowse->pluck('user_id');
        }
        $user_avatar = User::whereIn('id', $crowds)->get(['id as user_id', 'avatar']);

        return [
            'id' => $this->id,
            'shop_type' => $this->shop_type,
            'area' => $this->area,
            'promoter' => $this->promoter,
            'shop_info' => $img_arr,
            'shop_price' => $this->shop_price,
            'has_crowd' => $this->shopcrowd->sum('money') . '.00',
            'address' => $this->address,
            'rest_time' => timediff(time(), $this->crowd_funding_end),
            'user_avatar' => $user_avatar,
            'status' => $this->status
        ];
    }

    public function transform_my_shop_list()
    {
        $img_arr = [];
        foreach ($this->shop_info as $value) {
            $img_arr[] = config('app.img_url') . $value;
        }
        $label = UserLabel::whereIn('id', explode(',', $this->label))->get()->pluck('name');
        if(count($this->shopbrowse)>4){
            $crowds = $this->shopbrowse->random(4)->pluck('user_id');
        }else{
            $crowds = $this->shopbrowse->pluck('user_id');
        }
        $user_avatar = User::whereIn('id', $crowds)->get(['id as user_id', 'avatar']);
        return [
            'id' => $this->id,
            'shop_type' => $this->shop_type,
            'area' => $this->area,
            'promoter' => $this->promoter,
            'shop_info' => $img_arr,
            'shop_price' => $this->shop_price,
            'has_crowd' => $this->shopcrowd->sum('money') . '.00',
            'address' => $this->address,
            'rest_time' => timediff(time(), $this->crowd_funding_end),
            'label' => $label,
            'user_avatar' => $user_avatar
        ];
    }
    //线上众筹
    public function transform_shop_detail_line()
    {
        $img_arr = [];
        foreach ($this->shop_info as $value) {
            $img_arr[] = config('app.img_url') . $value;
        }
        $step1 = $this->shopcrowd->whereIn('crowd_type', [0])->pluck('user_id');
        $step1_img = User::whereIn('id', $step1)->get(['id as user_id', 'avatar']);

        $step2 = $this->shopcrowd->whereIn('crowd_type', [1])->pluck('user_id');
        $step2_img = User::whereIn('id', $step2)->get(['id as user_id', 'avatar']);
        $step3 = $this->shopcrowd->whereIn('crowd_type', [2])->pluck('user_id');
        $step3_img = User::whereIn('id', $step3)->get(['id as user_id', 'avatar']);

        $label = UserLabel::whereIn('id', explode(',', $this->label))->get()->pluck('name');
        $user_avatar = User::where('id', $this->user_id)->value('avatar');
        return [
            'id' => $this->id,
            'area' => $this->area,
            'promoter' => $this->promoter,
            'shop_info' => $img_arr,
            'shop_price' => $this->shop_price,
            'has_crowd' => $this->shopcrowd->sum('money') . '.00',
            'address' => $this->address,
            'step500' => $this->step1,
            'step500_has' => count($step1),
            'step500_img' => $step1_img,
            'step1300' => $this->step2,
            'step1300_has' => count($step2),
            'step1300_img' => $step2_img,
            'step5000' => $this->step3,
            'step5000_has' => count($step3),
            'step5000_img' => $step3_img,
            'start_time' => $this->created_at->toDateString(),
            'user_avatar' => $user_avatar,
            'label' => $label,
            'rest_time' => timediff(time(), $this->crowd_funding_end),
        ];
    }
    //线下众筹
    public function transform_shop_detail()
    {
        $img_arr = [];
        foreach ($this->shop_info as $value) {
            $img_arr[] = config('app.img_url') . $value;
        }
        $step1 = $this->shopcrowd->whereIn('crowd_type', [0])->pluck('user_id');
        $step1_img = User::whereIn('id', $step1)->get(['id as user_id', 'avatar']);

        $step2 = $this->shopcrowd->whereIn('crowd_type', [1])->pluck('user_id');
        $step2_img = User::whereIn('id', $step2)->get(['id as user_id', 'avatar']);
        $step3 = $this->shopcrowd->whereIn('crowd_type', [2])->pluck('user_id');
        $step3_img = User::whereIn('id', $step3)->get(['id as user_id', 'avatar']);

        $role_dz = $this->shoprole->whereIn('role', [1])->pluck('user_id');
        
        $role_dz_arr = User::whereIn('id', $role_dz)->get(['id as user_id', 'avatar']);

        $role_dy = $this->shoprole->whereIn('role', [2])->pluck('user_id');
        $role_dy_arr = User::whereIn('id', $role_dy)->get(['id as user_id', 'avatar']);
        $role_mt = $this->shoprole->whereIn('role', [3])->pluck('user_id');
        $role_mt_arr = User::whereIn('id', $role_mt)->get(['id as user_id', 'avatar']);
        $label = UserLabel::whereIn('id', explode(',', $this->label))->get()->pluck('name');
        $user_avatar = User::where('id', $this->user_id)->value('avatar');
        return [
            'id' => $this->id,
            'area' => $this->area,
            'promoter' => $this->promoter,
            'shop_info' => $img_arr,
            'shop_price' => $this->shop_price,
            'has_crowd' => $this->shopcrowd->sum('money') . '.00',
            'address' => $this->address,
            'role_dz' => $role_dz_arr,
            'role_dy' => $role_dy_arr,
            'role_mt' => $role_mt_arr,
            'step500' => $this->step1,
            'step500_has' => count($step1),
            'step500_img' => $step1_img,
            'step1300' => $this->step2,
            'step1300_has' => count($step2),
            'step1300_img' => $step2_img,
            'step5000' => $this->step3,
            'step5000_has' => count($step3),
            'step5000_img' => $step3_img,
            'start_time' => $this->created_at->toDateString(),
            'user_avatar' => $user_avatar,            
            'label' => $label,
            'rest_time' => timediff(time(), $this->crowd_funding_end),
        ];
    }
    public function setShopInfoAttribute($ShopInfo)
    {
        if (is_array($ShopInfo)) {
            $this->attributes['shop_info'] = json_encode($ShopInfo);
        }
    }

    public function getShopInfoAttribute($ShopInfo)
    {
        return json_decode($ShopInfo, true);
    }

    public function shoprole()
    {
        return $this->hasMany(ShopRole::class, 'shop_id', 'id');
    }

    public function shopcrowd()
    {
        return $this->hasMany(ShopCrowd::class, 'shop_id', 'id');
    }

    public function shopbrowse()
    {
        return $this->hasMany(ShopBrowse::class, 'shop_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(user::class);
    }

    public function shopprofit()
    {
        return $this->hasMany(ShopProfit::class, 'shop_id', 'id');
    }

    public function rentcloth()
    {
        return $this->hasOne(RentCloth::class,'shop_id','id');
    }

}
